package wiki_playground_dimitar;               
//import java.io.FileOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import processing.core.*; 
import processing.xml.XMLWriter;

import javax.xml.parsers.*;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
//import javax.xml.stream.XMLOutputFactory;
//import javax.xml.stream.XMLStreamWriter;
import javax.xml.xpath.*;

public class _7_xpathX_createNewXML_and_simplify {
 PApplet parent;

 static float [] memoryLat = new float [2]; 
 static float [] memoryLng= new float [2]; 
 public static float aPunkt; 
 static float bPunkt;
 static float deviation = 0.0001f; 
 
 _7_xpathX_createNewXML_and_simplify(PApplet p) {
	   parent = p; 	  	
}
 
public static void main(String[] args) 
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException, Exception
          
  {
	/*<parsing doc with JAXP --> standard >*/
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("data/gps_test_medium.xml");
    
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();				
    XPathExpression latExpr = xpath.compile("//@lat"); 
    XPathExpression lngExpr = xpath.compile("//@lng"); 
    
    /*Great XPath-objects and Nodelists
     * 
     */
    Object latResult = latExpr.evaluate(doc, XPathConstants.NODESET); 	
    Object lngResult = lngExpr.evaluate(doc, XPathConstants.NODESET); 
    NodeList latNodes = (NodeList) latResult;	
    NodeList lngNodes = (NodeList) lngResult; 
  
    /*save the first position (lat/lng) in Array[2] on place 0)*/
    memoryLat[0] = new Float (latNodes.item(0).getNodeValue()); 
    memoryLng[0] = new Float (lngNodes.item(0).getNodeValue());
  
    /*create the XML file */  
    XMLOutputFactory myFactory = XMLOutputFactory.newInstance(); 
    XMLStreamWriter writer = myFactory.createXMLStreamWriter(new FileOutputStream("data/newDoc.xml")); //create document
    writer.writeStartDocument(); 
    writer.writeStartElement("locations");  // write <root> element;  
  	
	
    for (int i= 1; i < latNodes.getLength(); i++) //i = 1; i=0 is already in memory[0]
    {	
    	/*insert for every Node the value in memory[1] */
    	memoryLat[1]=new Float (latNodes.item(i).getNodeValue()); 
    	memoryLng[1]=new Float (lngNodes.item(i).getNodeValue()); 
    	/* if the new value memory[1] of the item has a bigger diviation than 0.002 than ... */
    	if (
    			memoryLat[1] >= memoryLat[0]+deviation || 
    			memoryLat[1] <= memoryLat[0]-deviation || 
    			memoryLng[1] >= memoryLng[0]+deviation || 
    			memoryLng[1] <= memoryLng[0]-deviation)
    		{
    			/* ... take the value of [1] and make it starting position for the next comparision*/ 
    			memoryLat[0] = memoryLat[1]; 
    			String memoryLatString = Float.toString(memoryLat[1]);
    			
    			memoryLng[0] = memoryLng[1]; 
    			String memoryLngString = Float.toString(memoryLng[1]);
    			

    			/*write the positions in the XML doc*/ 		
    			writer.writeStartElement("location"); 		  //write first child 
    			writer.writeAttribute("lat", memoryLatString ); //create Attribute; hasn't to be closed
    			writer.writeAttribute("lng", memoryLngString); // create another attribute 
    			writer.writeEndElement(); //end Attripute-tag; 
    			//System.out.println(memoryLat[1]);

    		} //close if
    	}//close for-loop 

    	/*close the XML Document*/ 
    	writer.writeEndElement(); //close <root>;
    	writer.writeEndDocument(); 
    	writer.close();
    	System.out.println("file successfull created");
  
          }
  }

  
         
  




	
	
